package com.insurance.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.insurance.common.common.ResponseData;
import com.insurance.common.enums.CommonEnum;
import com.insurance.product.dto.ProductDto;
import com.insurance.product.entity.ProductEntity;
import com.insurance.product.service.ProductService;
import com.insurance.productcase.dto.ProductCaseDto;
import com.insurance.productcase.entity.ProductCaseEntity;
import com.insurance.productcase.service.ProductCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Style
 * @version 1.0
 * @date 2021/3/11
 **/
//@Api(tags = "产品服务")
@Slf4j
//@RestController
//@RequestMapping("/product")
public class ProductController {

    /**
     * 产品service
     */
    @Autowired
    private ProductService productService;

    /**
     * 方案service
     */
    @Autowired
    private ProductCaseService productCaseService;

    /**
     * 通过产品编码查询产品
     *
     * @param productCode 产品编码
     * @return
     */
    @ApiOperation(value = "通过产品编码查询产品")
    @PostMapping("/getProductInfoByCode/{productCode}")
    public ResponseData<ProductDto> getProductInfoByCode(@PathVariable String productCode, @RequestHeader(value = "Authorization") String token){
        try {
            ProductEntity productEntity = productService.getProductByCode(productCode);
            if(productEntity != null){
                ProductDto productDto = new ProductDto();
                BeanUtils.copyProperties(productEntity, productDto);
                return ResponseData.success(productDto);
            }
            return ResponseData.error(CommonEnum.QUERY_ERROR_NODATA);
        } catch (Exception e) {
            log.error("【通过产品编码查询产品失败, 系统异常】", e);
            return ResponseData.error(CommonEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 通过产品编码查询方案
     *
     * @param productCode 产品编码
     * @return
     */
    @ApiOperation(value = "通过产品编码查询方案")
    @PostMapping("/queryProductCaseByCode/{productCode}")
    public ResponseData<List<ProductCaseDto>> queryProductCaseByCode(@PathVariable String productCode, @RequestHeader(value = "Authorization") String token){
        try {
            //通过产品编码查询产品
            ProductEntity productEntity = productService.getProductByCode(productCode);
            if(productEntity == null){
                log.error("【产品编码：{}, 查询产品方案失败, 产品不存在】", productCode);
                return ResponseData.error(CommonEnum.QUERY_ERROR_NODATA);
            }
            //通过产品主键查询方案
            List<ProductCaseEntity> productCaseEntityList = productCaseService.queryListByProductId(productEntity.getProductId());
            List<ProductCaseDto> productCaseDtoList = new ArrayList<ProductCaseDto>();
            if(CollectionUtil.isNotEmpty(productCaseEntityList)){
                productCaseEntityList.forEach(productCaseEntity -> {
                    ProductCaseDto productCaseDto = new ProductCaseDto();
                    BeanUtils.copyProperties(productCaseEntity, productCaseDto);
                    productCaseDtoList.add(productCaseDto);
                });
            }
            return ResponseData.success(productCaseDtoList);
        } catch (Exception e) {
            log.error("【产品编码：{}, 查询产品方案失败, 系统异常：{}】", productCode, e.getMessage());
            return ResponseData.error(CommonEnum.SYSTEM_ERROR);
        }
    }

}
